package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Text;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class ResultsIndex extends Activity implements OnClickListener {

	TextView firstIntake;
	TextView secondIntake;
	TextView thirdIntake;
	
	String batch1Value;
	String batch2Value;
	String batch3Value;
	String scriptVal1;
	String scriptVal2;
	String scriptVal3;
	String studNumber;
	String SID;
	String icpassport;
	
	String scriptVal1tmp1;
	String scriptVal2tmp1;
	String scriptVal3tmp1;
	
	String scriptVal1tmp2;
	String scriptVal2tmp2;
	String scriptVal3tmp2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_results_index);

		firstIntake = (TextView) findViewById(R.id.firstIntake);
		secondIntake = (TextView) findViewById(R.id.secondIntake);
		thirdIntake = (TextView) findViewById(R.id.thirdIntake);

		firstIntake.setOnClickListener(this);
		secondIntake.setOnClickListener(this);
		thirdIntake.setOnClickListener(this);
		
		Intent intename = getIntent();
		batch1Value = intename.getStringExtra("batch1");
		batch2Value = intename.getStringExtra("batch2");
		batch3Value = intename.getStringExtra("batch3");
		scriptVal1 = intename.getStringExtra("scriptVal1");
		scriptVal2 = intename.getStringExtra("scriptVal2");
		scriptVal3 = intename.getStringExtra("scriptVal3");
		studNumber = intename.getStringExtra("studNumber");
		SID = intename.getStringExtra("SID");
		icpassport = intename.getStringExtra("icpassport");
		
		firstIntake.setText(batch1Value);
		secondIntake.setText(batch2Value);
		thirdIntake.setText(batch3Value);
		
		scriptVal1tmp1 = scriptVal1.substring(41);
		scriptVal1tmp2 = scriptVal1tmp1.replaceAll("[) ; ' ]", "");
		
		scriptVal2tmp1 = scriptVal2.substring(41);
		scriptVal2tmp2 = scriptVal2tmp1.replaceAll("[) ; ' ]", "");
		
		scriptVal3tmp1 = scriptVal3.substring(41);
		scriptVal3tmp2 = scriptVal3tmp1.replaceAll("[) ; ' ]", "");
		
//		firstIntake.setText(scriptVal1tmp2);
//		secondIntake.setText(scriptVal2tmp2);
//		thirdIntake.setText(scriptVal3tmp2);

		
		Toast.makeText(getApplicationContext(), scriptVal1tmp2,
				Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.results_index, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == firstIntake) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/result/mainresults.asp?STUDENT_NUMBER="+ studNumber+ "&SID="+ SID);
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("module1", "/html[1]/body[1]/table[4]/tr[3]/td[1]/font[1]---");
			xpathMap.put("module2", "/html[1]/body[1]/table[4]/tr[4]/td[1]/font[1]---");
			xpathMap.put("module3", "/html[1]/body[1]/table[4]/tr[5]/td[1]/font[1]---");
			xpathMap.put("module4", "/html[1]/body[1]/table[4]/tr[6]/td[1]/font[1]---");
			xpathMap.put("module5", "/html[1]/body[1]/table[4]/tr[7]/td[1]/font[1]---");
			xpathMap.put("module6", "/html[1]/body[1]/table[4]/tr[8]/td[1]/font[1]---");
			xpathMap.put("module7", "/html[1]/body[1]/table[4]/tr[9]/td[1]/font[1]---");
			xpathMap.put("module8", "/html[1]/body[1]/table[4]/tr[10]/td[1]/font[1]---");
			
			xpathMap.put("result1", "/html[1]/body[1]/table[4]/tr[3]/td[2]/font[1]---");
			xpathMap.put("result2", "/html[1]/body[1]/table[4]/tr[4]/td[2]/font[1]---");
			xpathMap.put("result3", "/html[1]/body[1]/table[4]/tr[5]/td[2]/font[1]---");
			xpathMap.put("result4", "/html[1]/body[1]/table[4]/tr[6]/td[2]/font[1]---");
			xpathMap.put("result5", "/html[1]/body[1]/table[4]/tr[7]/td[2]/font[1]---");
			xpathMap.put("result6", "/html[1]/body[1]/table[4]/tr[8]/td[2]/font[1]---");
			xpathMap.put("result7", "/html[1]/body[1]/table[4]/tr[9]/td[2]/font[1]---");
			xpathMap.put("result8", "/html[1]/body[1]/table[4]/tr[10]/td[2]/font[1]---");
			request.setXpathMap(xpathMap);

			// campus=CB&appraisal=&student_number=CB004229&icpassport=CB0000027003&SID=883ca5716250a95604bc8870c8e24d79&intake=SUL3SEENG-20130201-FullTime-1
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("campus", "CB");
			parametersMap.put("student_number", studNumber);
			parametersMap.put("SID", SID);
			parametersMap.put("icpassport",icpassport);
			parametersMap.put("appraisal","");
			parametersMap.put("intake",scriptVal1tmp2);
			request.setParametersMap(parametersMap);

			request.setType("POST");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String module1 = respones.getResultsMap().get("module1").replace("&nbsp;", "");
			String module2 = respones.getResultsMap().get("module2").replace("&nbsp;", "");
			String module3 = respones.getResultsMap().get("module3").replace("&nbsp;", "");
			String module4 = respones.getResultsMap().get("module4").replace("&nbsp;", "");
			String module5 = respones.getResultsMap().get("module5").replace("&nbsp;", "");
			String module6 = respones.getResultsMap().get("module6").replace("&nbsp;", "");
			String module7 = respones.getResultsMap().get("module7").replace("&nbsp;", "");
			String module8 = respones.getResultsMap().get("module8").replace("&nbsp;", "");
			
			String result1 = respones.getResultsMap().get("result1").replace("&nbsp;", "");
			String result2 = respones.getResultsMap().get("result2").replace("&nbsp;", "");
			String result3 = respones.getResultsMap().get("result3").replace("&nbsp;", "");
			String result4 = respones.getResultsMap().get("result4").replace("&nbsp;", "");
			String result5 = respones.getResultsMap().get("result5").replace("&nbsp;", "");
			String result6 = respones.getResultsMap().get("result6").replace("&nbsp;", "");
			String result7 = respones.getResultsMap().get("result7").replace("&nbsp;", "");
			String result8 = respones.getResultsMap().get("result8").replace("&nbsp;", "");

			Toast.makeText(getApplicationContext(), module1,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, Results.class);
			i.putExtra("module1", module1);
			i.putExtra("module2", module2);
			i.putExtra("module3", module3);
			i.putExtra("module4", module4);
			i.putExtra("module5", module5);
			i.putExtra("module6", module6);
			i.putExtra("module7", module7);
			i.putExtra("module8", module8);
			i.putExtra("result1", result1);
			i.putExtra("result2", result2);
			i.putExtra("result3", result3);
			i.putExtra("result4", result4);
			i.putExtra("result5", result5);
			i.putExtra("result6", result6);
			i.putExtra("result7", result7);
			i.putExtra("result8", result8);
			startActivity(i);
		}
		if (v == secondIntake) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/result/mainresults.asp?STUDENT_NUMBER="+ studNumber+ "&SID="+ SID);
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("module1", "/html[1]/body[1]/table[4]/tr[3]/td[1]/font[1]---");
			xpathMap.put("module2", "/html[1]/body[1]/table[4]/tr[4]/td[1]/font[1]---");
			xpathMap.put("module3", "/html[1]/body[1]/table[4]/tr[5]/td[1]/font[1]---");
			xpathMap.put("module4", "/html[1]/body[1]/table[4]/tr[6]/td[1]/font[1]---");
			xpathMap.put("module5", "/html[1]/body[1]/table[4]/tr[7]/td[1]/font[1]---");
			xpathMap.put("module6", "/html[1]/body[1]/table[4]/tr[8]/td[1]/font[1]---");
			xpathMap.put("module7", "/html[1]/body[1]/table[4]/tr[9]/td[1]/font[1]---");
			xpathMap.put("module8", "/html[1]/body[1]/table[4]/tr[10]/td[1]/font[1]---");
			
			xpathMap.put("result1", "/html[1]/body[1]/table[4]/tr[3]/td[2]/font[1]---");
			xpathMap.put("result2", "/html[1]/body[1]/table[4]/tr[4]/td[2]/font[1]---");
			xpathMap.put("result3", "/html[1]/body[1]/table[4]/tr[5]/td[2]/font[1]---");
			xpathMap.put("result4", "/html[1]/body[1]/table[4]/tr[6]/td[2]/font[1]---");
			xpathMap.put("result5", "/html[1]/body[1]/table[4]/tr[7]/td[2]/font[1]---");
			xpathMap.put("result6", "/html[1]/body[1]/table[4]/tr[8]/td[2]/font[1]---");
			xpathMap.put("result7", "/html[1]/body[1]/table[4]/tr[9]/td[2]/font[1]---");
			xpathMap.put("result8", "/html[1]/body[1]/table[4]/tr[10]/td[2]/font[1]---");
			request.setXpathMap(xpathMap);

			// campus=CB&appraisal=&student_number=CB004229&icpassport=CB0000027003&SID=883ca5716250a95604bc8870c8e24d79&intake=SUL3SEENG-20130201-FullTime-1
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("campus", "CB");
			parametersMap.put("student_number", studNumber);
			parametersMap.put("SID", SID);
			parametersMap.put("icpassport",icpassport);
			parametersMap.put("appraisal","");
			parametersMap.put("intake",scriptVal2tmp2);
			request.setParametersMap(parametersMap);

			request.setType("POST");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String module1 = respones.getResultsMap().get("module1").replace("&nbsp;", "");
			String module2 = respones.getResultsMap().get("module2").replace("&nbsp;", "");
			String module3 = respones.getResultsMap().get("module3").replace("&nbsp;", "");
			String module4 = respones.getResultsMap().get("module4").replace("&nbsp;", "");
			String module5 = respones.getResultsMap().get("module5").replace("&nbsp;", "");
			String module6 = respones.getResultsMap().get("module6").replace("&nbsp;", "");
			String module7 = respones.getResultsMap().get("module7").replace("&nbsp;", "");
			String module8 = respones.getResultsMap().get("module8").replace("&nbsp;", "");
			
			String result1 = respones.getResultsMap().get("result1").replace("&nbsp;", "");
			String result2 = respones.getResultsMap().get("result2").replace("&nbsp;", "");
			String result3 = respones.getResultsMap().get("result3").replace("&nbsp;", "");
			String result4 = respones.getResultsMap().get("result4").replace("&nbsp;", "");
			String result5 = respones.getResultsMap().get("result5").replace("&nbsp;", "");
			String result6 = respones.getResultsMap().get("result6").replace("&nbsp;", "");
			String result7 = respones.getResultsMap().get("result7").replace("&nbsp;", "");
			String result8 = respones.getResultsMap().get("result8").replace("&nbsp;", "");

			Toast.makeText(getApplicationContext(), module1,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, Results.class);
			i.putExtra("module1", module1);
			i.putExtra("module2", module2);
			i.putExtra("module3", module3);
			i.putExtra("module4", module4);
			i.putExtra("module5", module5);
			i.putExtra("module6", module6);
			i.putExtra("module7", module7);
			i.putExtra("module8", module8);
			i.putExtra("result1", result1);
			i.putExtra("result2", result2);
			i.putExtra("result3", result3);
			i.putExtra("result4", result4);
			i.putExtra("result5", result5);
			i.putExtra("result6", result6);
			i.putExtra("result7", result7);
			i.putExtra("result8", result8);
			startActivity(i);
		}
		if (v == thirdIntake) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/result/mainresults.asp?STUDENT_NUMBER="+ studNumber+ "&SID="+ SID);
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("module1", "/html[1]/body[1]/table[4]/tr[3]/td[1]/font[1]---");
			xpathMap.put("module2", "/html[1]/body[1]/table[4]/tr[4]/td[1]/font[1]---");
			xpathMap.put("module3", "/html[1]/body[1]/table[4]/tr[5]/td[1]/font[1]---");
			xpathMap.put("module4", "/html[1]/body[1]/table[4]/tr[6]/td[1]/font[1]---");
			xpathMap.put("module5", "/html[1]/body[1]/table[4]/tr[7]/td[1]/font[1]---");
			xpathMap.put("module6", "/html[1]/body[1]/table[4]/tr[8]/td[1]/font[1]---");
			xpathMap.put("module7", "/html[1]/body[1]/table[4]/tr[9]/td[1]/font[1]---");
			xpathMap.put("module8", "/html[1]/body[1]/table[4]/tr[10]/td[1]/font[1]---");
			
			xpathMap.put("result1", "/html[1]/body[1]/table[4]/tr[3]/td[2]/font[1]---");
			xpathMap.put("result2", "/html[1]/body[1]/table[4]/tr[4]/td[2]/font[1]---");
			xpathMap.put("result3", "/html[1]/body[1]/table[4]/tr[5]/td[2]/font[1]---");
			xpathMap.put("result4", "/html[1]/body[1]/table[4]/tr[6]/td[2]/font[1]---");
			xpathMap.put("result5", "/html[1]/body[1]/table[4]/tr[7]/td[2]/font[1]---");
			xpathMap.put("result6", "/html[1]/body[1]/table[4]/tr[8]/td[2]/font[1]---");
			xpathMap.put("result7", "/html[1]/body[1]/table[4]/tr[9]/td[2]/font[1]---");
			xpathMap.put("result8", "/html[1]/body[1]/table[4]/tr[10]/td[2]/font[1]---");
			request.setXpathMap(xpathMap);

			// campus=CB&appraisal=&student_number=CB004229&icpassport=CB0000027003&SID=883ca5716250a95604bc8870c8e24d79&intake=SUL3SEENG-20130201-FullTime-1
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("campus", "CB");
			parametersMap.put("student_number", studNumber);
			parametersMap.put("SID", SID);
			parametersMap.put("icpassport",icpassport);
			parametersMap.put("appraisal","");
			parametersMap.put("intake",scriptVal3tmp2);
			request.setParametersMap(parametersMap);

			request.setType("POST");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String module1 = respones.getResultsMap().get("module1").replace("&nbsp;", "");
			String module2 = respones.getResultsMap().get("module2").replace("&nbsp;", "");
			String module3 = respones.getResultsMap().get("module3").replace("&nbsp;", "");
			String module4 = respones.getResultsMap().get("module4").replace("&nbsp;", "");
			String module5 = respones.getResultsMap().get("module5").replace("&nbsp;", "");
			String module6 = respones.getResultsMap().get("module6").replace("&nbsp;", "");
			String module7 = respones.getResultsMap().get("module7").replace("&nbsp;", "");
			String module8 = respones.getResultsMap().get("module8").replace("&nbsp;", "");
			
			String result1 = respones.getResultsMap().get("result1").replace("&nbsp;", "");
			String result2 = respones.getResultsMap().get("result2").replace("&nbsp;", "");
			String result3 = respones.getResultsMap().get("result3").replace("&nbsp;", "");
			String result4 = respones.getResultsMap().get("result4").replace("&nbsp;", "");
			String result5 = respones.getResultsMap().get("result5").replace("&nbsp;", "");
			String result6 = respones.getResultsMap().get("result6").replace("&nbsp;", "");
			String result7 = respones.getResultsMap().get("result7").replace("&nbsp;", "");
			String result8 = respones.getResultsMap().get("result8").replace("&nbsp;", "");

			Toast.makeText(getApplicationContext(), module1,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, Results.class);
			i.putExtra("module1", module1);
			i.putExtra("module2", module2);
			i.putExtra("module3", module3);
			i.putExtra("module4", module4);
			i.putExtra("module5", module5);
			i.putExtra("module6", module6);
			i.putExtra("module7", module7);
			i.putExtra("module8", module8);
			i.putExtra("result1", result1);
			i.putExtra("result2", result2);
			i.putExtra("result3", result3);
			i.putExtra("result4", result4);
			i.putExtra("result5", result5);
			i.putExtra("result6", result6);
			i.putExtra("result7", result7);
			i.putExtra("result8", result8);
			startActivity(i);
		}

	}

}
